extends CharacterBody2D_Unit


class_name EnemyUnit


@onready var move: MoveComponent = $CharacterComponent/Move/MoveComponent
@onready var animated_sprite_2d: AnimatedSprite2D = $AnimatedSprite2D
@onready var navigation_agent_2d: NavigationAgent2D = $NavigationAgent2D
@onready var navigation_element: NavigationElement = $CharacterComponent/Move/MoveComponent/NavigationElement
@onready var label: Label = $Node2D/Label
@onready var state_component: StateComponent = $StateComponent


enum State
{
    Idle,
    Move,
    Seek,
    Hurt,
    Attack,
    Dead,
    Invalid,
}

var state_desc: Array[String] = [
    "Idle",
    "Move",
    "Seek",
    "Hurt",
    "Attack",
    "Dead",
    "Invalid",
]


var body_shader: ShaderMaterial = null


func _ready() -> void:
    super._ready()

    body_shader = animated_sprite_2d.material as ShaderMaterial
    state_enter(State.Idle)

func __set_enable(v: bool) -> void:
    # true -> false
    if is_enable and not v:
        state_enter(State.Idle)
    super.__set_enable(v)

func on_message(character: CharacterComponent, e: CharacterEvent) -> void:
    super.on_message(character, e)
    match e.type:
        CharacterUnit.Event.ON_DIE:
            state_component.switch(State.Dead)

func on_effect(character: CharacterComponent, type: CharacterUnit.Effect, e: CharacterEvent) -> void:
    super.on_effect(character, type, e)
    match type:
        CharacterUnit.Effect.DO_DAMAGE:
            state_component.switch(State.Hurt)

func on_state(state: int, next: int, state_duration: float) -> void:
    #print("State:", state_desc[state], " => ", state_desc[next])
    state_exit(state)
    state_enter(next)

func state_enter(next: State) -> void:
    match next:
        State.Idle:
            NodeHelper.play_animation(animated_sprite_2d, "idle")
        State.Move:
            NodeHelper.play_animation(animated_sprite_2d, "move")
        State.Hurt:
            body_shader.set_shader_parameter("flash_active", true)
        State.Attack:
            NodeHelper.play_animation(animated_sprite_2d, "attack_right")
            body_shader.set_shader_parameter("width", 2.0)
        State.Dead:
            node_2d.visible = false
            NodeHelper.play_animation(animated_sprite_2d, "dead")
        State.Invalid:
            character_component.meta.items.hp.full()
            event_finished.emit(self)
    label.text = state_desc[next]

func state_exit(state: State) -> void:
    match state:
        State.Move:
            move.stop()
            NodeHelper.play_animation(animated_sprite_2d, "idle")
        State.Hurt:
            body_shader.set_shader_parameter("flash_active", false)
        State.Attack:
            body_shader.set_shader_parameter("width", 0.0)
        State.Dead:
            node_2d.visible = true

func update_state(prev: int, state: int, delta: float, state_duration: float) -> int:
    match state:
        State.Idle:
            if state_duration > 2:
                return State.Seek
        State.Seek:
            #navigation_element.seek(get_global_mouse_position())
            return State.Move
        State.Move:
            if state_duration > 1.0:
                return State.Attack
        State.Hurt:
            if state_duration > 0.05:
                return prev
        State.Attack:
            if not animated_sprite_2d.is_playing():
                return State.Idle
        State.Dead:
            if not animated_sprite_2d.is_playing():
                return State.Invalid
        State.Invalid:
            if is_enable:
                return State.Idle
    return state
